package com.tweet_o_matic.activities;

import static com.tweet_o_matic.helpers.IntentHelper.selectContact;
import static com.tweet_o_matic.helpers.IntentHelper.sendEmail;
import static com.tweet_o_matic.helpers.ContactHelper.getDisplayName;

import java.text.SimpleDateFormat;
import java.util.UUID;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tweet_o_matic.R;
import com.tweet_o_matic.main.TweetApp;
import com.tweet_o_matic.models.Tweet;
import com.tweet_o_matic.models.TweetPortfolio;



public class TweetActivity2 extends ActionBarActivity implements TextWatcher, OnCheckedChangeListener, OnClickListener{
	
	EditText enterText;
	TextView charCount;
	TextView dateView;
	Button tweetButton;
	Tweet tweet;
	TweetPortfolio tweetPortfolio;
	Button selectContact;
	Button emailTweetVia;
	
	private static final int REQUEST_CONTACT = 1;


	
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getActionBar().setDisplayHomeAsUpEnabled(false);
		
		enterText 		= (EditText)	findViewById(R.id.enterText);
		charCount		= (TextView)	findViewById(R.id.CharCount);
		dateView		= (TextView)	findViewById(R.id.dateView);
		tweetButton 	= (Button) 		findViewById(R.id.tweetButton);
		selectContact	= (Button)   	findViewById(R.id.selectContact);
		emailTweetVia 	= (Button)   	findViewById(R.id.emailTweetVia);
		
		tweet = new Tweet();
		
		enterText.addTextChangedListener(this);
		selectContact.setOnClickListener(this);
	    emailTweetVia.setOnClickListener(this);
		tweetButton.setOnClickListener(this);
		
	    TweetApp app = (TweetApp) getApplication();
	    tweetPortfolio = app.tweetPortfolio;		
		
		long date = System.currentTimeMillis(); 

		SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:mm a");
		String dateString = sdf.format(date);  
		
		dateView.setText(dateString);
		
		
	    UUID twtId = (UUID) getIntent().getExtras().getSerializable("TWEET_ID");
	    tweet = tweetPortfolio.getTweet(twtId);
	    if (tweet != null)
	    {
	      updateControls(tweet);
	    }	
		
		
		final TextWatcher enterTextWatcher = new TextWatcher(){
			
	        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	        }

	        public void onTextChanged(CharSequence s, int start, int before, int count) {
				charCount.setText(String.valueOf(140 - s.length()));
	        }

	        public void afterTextChanged(Editable s) {
	        }
		};
		
		enterText.addTextChangedListener(enterTextWatcher);
		
		
		

	}
	
	public void onTweet(){
	   	  String text;
		  Integer length;
		  text = enterText.getText().toString();
		  length = text.length();
		  
		  if((length > 0) && (length < 140)){
		  Toast.makeText(getBaseContext(), "Tweet Sent.", Toast.LENGTH_SHORT).show();
		  }else if(length > 140){
		  Toast.makeText(getBaseContext(), "140 characters max.", Toast.LENGTH_SHORT).show();
		  }else{    
		  Toast.makeText(getBaseContext(), "Can't send an empty tweet!", Toast.LENGTH_SHORT).show();
		  }
		};  
	


	public void onClick(View v){
		 switch (v.getId())
		   {
		     case R.id.selectContact : selectContact(this, REQUEST_CONTACT);
		     break;                                
		     case R.id.emailTweetVia : sendEmail(this, "", getString(R.string.tweet_report_subject), tweet.getTweetReport(this));                           
		     break;  
		     case R.id.tweetButton	 : onTweet();
		     break;      
		                                         
		   }
		  
	  }
	 
//});	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
	    switch (requestCode)
	    {
	      case REQUEST_CONTACT    : if(data == null) {
	    	  						  break;
	      							}
	      							String name = getDisplayName(this, data);
	                                tweet.contact = name;
	                                selectContact.setText(name);
	                                
	                                
	                                break;
	    }
	  } 
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tweet_add, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		else if(id == R.id.action_clear){
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	

	public void updateControls(Tweet tweet){
		enterText.setText(tweet.message);
		dateView.setText(tweet.getDateString());
	}
	
	public void onPause(){
	  super.onPause();
	  tweetPortfolio.saveTweets();
	}	
	  
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
	}


	  @Override
	  public void afterTextChanged(Editable c)
	  {
	    Log.i(this.getClass().getSimpleName(), "Message " + c.toString());
	    tweet.message = c.toString();
	  }


	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}



	

}
